# - Try to find HDF5 library
# Once done, this will define
#  HDF5_FOUND
#  HDF5_INCLUDE_DIRS
#  HDF5_LIBRARIES

message(STATUS "")
message(STATUS "finding HDF5 library")

# find where is "h5dump"
if(os STREQUAL "Mac OS X" OR os STREQUAL "Linux")
    execute_process(
        COMMAND which h5dump
        OUTPUT_VARIABLE h5dump_path
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
else(os STREQUAL "Mac OS X" OR os STREQUAL "Linux")
    message(FATAL_ERROR "Unsupported platform \"${OS}\"")
endif(os STREQUAL "Mac OS X" OR os STREQUAL "Linux")

if(h5dump_path STREQUAL "")
    set(HDF5_FOUND false)
    if(DEFINED HDF5_FIND_REQUIRED)
        message(FATAL_ERROR "HDF5 is required, but not found")
    endif(DEFINED HDF5_FIND_REQUIRED)
endif(h5dump_path STREQUAL "")

# use "h5dump" as a hint to get the path of HDF5
execute_process(
    COMMAND echo ${h5dump_path}
    COMMAND sed -e "s/\\/bin.*$//"
    OUTPUT_VARIABLE HDF5_PATH
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

set(HDF5_INCLUDE_DIR ${HDF5_PATH}/include)
set(HDF5_LIBRARY
    "-L${HDF5_PATH}/lib -lhdf5 -lhdf5_fortran"
)

set(HDF5_INCLUDE_DIRS ${HDF5_INCLUDE_DIR})
set(HDF5_LIBRARIES "${HDF5_LIBRARY} -lz")

message(STATUS "--> found HDF5 under ${HDF5_PATH}")

set(HDF5_FOUND true)
